package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.stream.events.Comment;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentService commentService;

    @PostMapping
    public ResponseEntity saveMovements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementsService.saveMovements(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")
    public ResponseEntity findByUserId(Integer page,Integer pagesize,Long userId){
        PageResult pageResult = movementsService.findByUserId(page,pagesize,userId);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementsService.movements(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementsService.recommend(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId){
        MovementsVo vo = movementsService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        Integer likeCount = commentService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId){
        Integer likeCount = commentService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId){
        Integer loveCount = commentService.loveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId){
        Integer loveCount = commentService.unloveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list =  movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}